#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<sys/select.h>




int main(int argc, char *argv[])
{
    char buf[128];
    int ret = 0;
    int fd =  open("/dev/hello", O_RDWR|O_NONBLOCK);
    if(fd < 0)
    {
        perror("open");
        exit(1);
    }
    printf("open success\n");
    printf("fd = %d\n", fd);

    fd_set readfds;
    while(1)
    {
        FD_ZERO(&readfds);
        FD_SET(STDIN_FILENO, &readfds);//标准输入
        FD_SET(fd, &readfds);//设备文件,监控hello
        ret = select(fd + 1, &readfds, NULL, NULL, NULL);
        if(ret < 0)
        {
            perror("select");
            exit(1);
        }
        for(int i = 0; i <= fd; i++)
        {
            if(FD_ISSET(i, &readfds))
            {
                if(i == STDIN_FILENO)
                {
                    fgets(buf, sizeof(buf), stdin);
                    printf("readfrom stdin:%s\n",buf);
                }
                else if(i == fd)
                {
                    if(read(i, buf, sizeof(buf)) < 0)
                    {
                        perror("read");
                        exit(1);
                    }
                    printf("readfrom dev:%s\n",buf);
                }
            }
        }       
    }
    close(fd);
    return 0;
}